package com.test.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.R;
import com.test.entity.Icon;

import java.util.List;

/**
 * 网图格式业务层
 *
 * @Author Musique
 * 2020/8/5
 */
public class GridViewAdapter extends BaseAdapter {

    private List<Icon> mIcon;
    private Context mContext;

    public GridViewAdapter(List<Icon> mIcon, Context mContext) {
        this.mIcon = mIcon;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mIcon.size();
    }

    @Override
    public Object getItem(int i) {
        return mIcon.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder = new ViewHolder();
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_gridview_demo, viewGroup, false);
            viewHolder.mImageView = view.findViewById(R.id.img_icon);
            viewHolder.mTextView = view.findViewById(R.id.text_icon);
            // 对象放入view.tag标签中
            view.setTag(viewHolder);
        }
        else {
            // 获取view里面的对象，并改变对象类型
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.mTextView.setText(mIcon.get(i).getTitle());
        viewHolder.mImageView.setBackgroundResource(mIcon.get(i).getAIcon());
        return view;
    }

    static class ViewHolder {
        private ImageView mImageView;
        private TextView mTextView;
    }
}
